/*
 * Copyright (c) 2011 Vasile Vilvoiu. All rights reserved.
 *   
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * thread.c - abstraction layer for thread management routines
 */
#include "thread.h"
#include "ferror.h"

/*
 * thread_create - create new thread
 *  returns: error text string, or NULL if code is undefined
 *   entry(in): entry point function
 *   param(in): entry point function parameter
 *   handle(out): create thread handle
 */
int thread_create(THREAD_ENTRY entry, THREAD_ENTRY_PARAM param, THREAD_HANDLE *handle) {

	int err = 0;

#if defined _WIN32 || defined _WIN64
	/* create thread */
	HANDLE hnd = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)entry, param, 0, NULL);
	
	/* copy to OUT param */
	if (handle != NULL) {
		*handle = hnd;
	}

	/* check thread */
	if (hnd == NULL) {
		err = GetLastError();
		return err_errorf(ERR_THREAD_CREATE_FAIL, "error code %d", __HERE__, err);
	}
#endif

#ifdef __linux__
#error not implemented yet
#endif

	/* all ok */
	return ERR_NONE;
}

/*
 * thread_yield - yield execution to other threads
 */
void thread_yield() {

#if defined _WIN32 || defined _WIN64
	SwitchToThread();
#endif

#ifdef __linux__
#error not implemented yet
#endif
}